package edu.princeton.cogsci.wn.ascii;

import java.util.*;
import edu.princeton.cogsci.wn.*;

public class AsciiWordSenseKey implements WordSenseKey
{
  // These members need package security so that AsciiWordNetImpl
  // can use them.

  String word;
  int pos;
  int category;
  int lexid;

  String headword = new String( );
  int headid;
  int offset;

  // int sense;

  AsciiWordSenseKey( String senseKey )
  {
    try
    {
      StringTokenizer st = new StringTokenizer( senseKey, "%:" );
      word = st.nextToken( );
      pos = Integer.parseInt( st.nextToken( ) );
      category = Integer.parseInt( st.nextToken( ) );
      lexid = Integer.parseInt( st.nextToken( ) );
      if ( st.hasMoreTokens( ) )
      {
        headword = st.nextToken( );
        headid = Integer.parseInt( st.nextToken( ) );
      }
    }
    catch ( NoSuchElementException nsee )
    {
      throw new WordNetException( "Malformed word sense key: " + senseKey );
    }
  }

  AsciiWordSenseKey( String word, int pos, int category, int lexid,
                     String headword, int headid, int offset )
  {
    this.word = word.toLowerCase( );
    this.pos = pos;
    this.category = category;
    this.lexid = lexid;
    this.headword = headword;
    this.headid = headid;
    this.offset = offset;
  }

  public String toString( )
  {
    return word + "%" + pos + ":" +
           ( category < 10 ? "0" : "" ) + category + ":" +
           ( lexid < 10 ? "0" : "" ) + lexid + ":" +
           headword + ":" +
           ( headword.length( ) > 0 ?
             ( headid < 10 ? "0" : "" ) + headid : "" );
  }

  public int hashCode( )
  {
    return toString( ).hashCode( );
  }

  public boolean equals( Object other )
  {
    return toString( ).equals( other.toString( ) );
  }

}


